package com.yaorange.module.system.controller;

import com.yaorange.module.system.service.SysUserService;
import com.yaorange.module.vo.SysUserPageVo;
import com.yaorange.pojo.SysUser;
import com.yaorange.response.PageResult;
import com.yaorange.response.Result;
import com.yaorange.response.enums.ResultCode;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 15332
 * @version 1.0
 * @date 2025/6/16 17:10
 * @description 用户类控制器
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

  @Autowired
  private SysUserService service;
  /**
   * 分页条件查询
   */
  @GetMapping
  public Result<PageResult<SysUser>> queryPage(SysUserPageVo userPageVo){
//    使用分页返回的一个类型进行接收
    PageResult<SysUser> pageResult = service.queryByCondition(userPageVo);
    return Result.of(ResultCode.SUCCESS,pageResult);
  }

  /**
   * 根据ID查询用户信息
   */
  @GetMapping("/{id}")
  public Result<SysUser> userList(@PathVariable(value = "id") Long id){
    SysUser user = service.selectById(id);
    return Result.of(ResultCode.SUCCESS,user);
  }

  /**
   * 添加用户
   */
  @PostMapping
  public Result<?> addUser(@Valid @RequestBody SysUser user){
    service.insert(user);
    return Result.of(ResultCode.SUCCESS);
  }

  /**
   * 修改用户
   */
@PutMapping
  public Result<?> updateUser(@Valid @RequestBody SysUser user){
  service.update(user);
  return Result.of(ResultCode.SUCCESS);
}

  /**
   * 删除用户
   */
  @DeleteMapping("/{id}")
  public Result<?> deleteUser(@PathVariable(value = "id")Long id){
    service.delete(id);
    return Result.of(ResultCode.SUCCESS);
  }

  /**
   * 为用户赋予角色
   * @param id 用户编号
   * @param roleId 角色编号列表
   * @return
   */
  @PostMapping("/{id}")
  public Result<?> grantRole(@PathVariable(value = "id") Long id, @RequestBody List<Long> roleId){
    service.grant(id,roleId);
    return Result.of(ResultCode.SUCCESS);
  }

  /**
   * 根据用户编号获取用户的角色信息
   * @param id
   * @return
   */
  @GetMapping("/{id}/role")
  public Result<List<Long>> roleById(@PathVariable("id") Long id){
    List<Long> roleList = service.select(id);
    return Result.success(roleList);
  }

}
